package org.noote.libs.spidy.anim;

import java.util.ArrayList;

import org.noote.libs.robot.Robot_ServoMotor;
import org.noote.libs.robot.wild.WildRobot_Animation;
import org.noote.libs.robot.wild.WildRobot_AnimationContext;

public class Spidy_WildRobot extends Spidy_BaseSkeleton {
	
	WildRobot_AnimationContext _animContext = null;
	
	public ArrayList<WildRobot_Animation> _anims = new ArrayList<WildRobot_Animation>();
	protected WildRobot_Animation _anim_default=null;
	
	public static final String FACTOR_SPEED_FRONT = "speed_front";
	public static final String FACTOR_ROTATION = "rotate";
	
	public Spidy_WildRobot() {
		super();
		
		_animContext = new WildRobot_AnimationContext(this);
		_animContext.newFactor(FACTOR_SPEED_FRONT).setValue(0);
		_animContext.newFactor(FACTOR_ROTATION).setValue(0);
		
		addDefaultAnim(new Spidy_WildAnim_Stand());
		addAnim(new Spidy_WildAnim_Walk_Tripod());
	}
	
	public boolean addDefaultAnim(WildRobot_Animation anim)
	{
		if(!addAnim(anim)) return false;
		_anim_default = anim;
		return true;
	}
	public boolean addAnim(WildRobot_Animation anim)
	{
		if(!anim.create()) return false;
		return _anims.add(anim);
	}
	
	public void goFront(float fSpeed)
	{
		fSpeed = fSpeed>1?1:fSpeed<0?0:fSpeed;
		_animContext.getFactor(FACTOR_SPEED_FRONT).setValue(fSpeed);
	}
	public void goBack(float fSpeed)
	{
		fSpeed = fSpeed>1?1:fSpeed<0?0:fSpeed;
		_animContext.getFactor(FACTOR_SPEED_FRONT).setValue(-fSpeed);
	}
	public void turnRight(float fRotate)
	{
		fRotate = fRotate>1?1:fRotate<0?0:fRotate;
		_animContext.getFactor(FACTOR_ROTATION).setValue(fRotate);
	}
	public void turnLeft(float fRotate)
	{
		fRotate = fRotate>1?1:fRotate<0?0:fRotate;
		_animContext.getFactor(FACTOR_ROTATION).setValue(-fRotate);
	}
	public void setNeutral()
	{
		_animContext.getFactor(FACTOR_SPEED_FRONT).setValue(0);
		_animContext.getFactor(FACTOR_ROTATION).setValue(0);
	}
	
	public boolean setDefaultPosition()
	{
		if(_anim_default == null) return false;
		_animContext.start(_anim_default);
		
		update();
		
		return true;
	}
	
	public void setAnimation(int iAnim)
	{
		if(iAnim>=0 && iAnim<=_anims.size())
		{
			_animContext.start(_anims.get(iAnim));
			for(Robot_ServoMotor s : getServoList())
			{
				s.flagUntransmitted();
			}
		}
	}

	public boolean isAnimationFinish() {
		return _animContext.isFinished();
	}

	public void doAnimation(float fTimeSlice) {
		_animContext.addTime(fTimeSlice);
	}	
}
